package service;

import commonUtil.CommonConst;
import commonUtil.CommonReturn;
import dao.CardInfoDAO;
import dataValueObject.CardInfoValue;
import dataValueObject.CustomerInfoValue;
import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * Created by xiezy3 on 2016/8/10.
 */
public class CardManager {
    static Logger logger = Logger.getLogger(CardManager.class);

    public static CardInfoValue[] queryCardInfo(int custId) {
        logger.info("CardManager.queryCardInfo Start with parameter custId :  " + custId);
        CardInfoDAO cardInfoDAO = new CardInfoDAO();
        CommonReturn commonReturn = new CommonReturn();
        CardInfoValue[] cardInfo = new CardInfoValue[1];

        if (custId <= 0) {
            commonReturn.setIsSuccess(false);
            commonReturn.setResultCode(CommonConst.ResultCode.PARAMETER_INVALIDATE);
            commonReturn.setResultMessage("Parameter invalidate");
            cardInfo[0].setCommonReturn(commonReturn);
            logger.error("queryCardInfo parameter invalidate");
            return cardInfo;
        }

        cardInfo = cardInfoDAO.queryCardInfoByCustId(custId);

        if (cardInfo.length == 0 || cardInfo[0].getCardId() <= 0) {
            commonReturn.setIsSuccess(true);
            commonReturn.setResultCode(CommonConst.ResultCode.NO_DATA_EXIST);
            commonReturn.setResultMessage("Cannot find cardInfo by custId : " + custId);
            cardInfo = new CardInfoValue[1];
            cardInfo[0] = new CardInfoValue();
            cardInfo[0].setCommonReturn(commonReturn);
            return cardInfo;
        }
        //查询卡片是否过期，过期置卡片失效
        if (checkExpireDate(cardInfo[0])) {
            cardInfoDAO.deleteCust(cardInfo[0]);
            commonReturn.setIsSuccess(true);
            commonReturn.setResultCode(CommonConst.ResultCode.DATA_NOT_EXPECT);
            commonReturn.setResultMessage("The card has been expired");
            cardInfo[0].setCommonReturn(commonReturn);
            return cardInfo;
        }

        commonReturn.setIsSuccess(true);
        commonReturn.setResultCode(CommonConst.ResultCode.SUCCESS);
        commonReturn.setResultMessage("Success");
        cardInfo[0].setCommonReturn(commonReturn);
        return cardInfo;
    }

    public static CommonReturn addCard(String cardType, int custId, String custName) {
        logger.info("CardManager.addCard Start with parameter cardType :  " + cardType + " custId : " + custId + " custName : " + custName);
        CommonReturn commonReturn = new CommonReturn();
        CardInfoDAO cardInfoDAO = new CardInfoDAO();
        if (!(CommonConst.TIMES_CARD.equals(cardType) || CommonConst.YEAR_CARD.equals(cardType)) || (custId <= 0 && custName == null)) {
            commonReturn.setIsSuccess(false);
            commonReturn.setResultCode(CommonConst.ResultCode.PARAMETER_INVALIDATE);
            commonReturn.setResultMessage("Parameter invalidate");
            logger.error("addCard parameter invalidate");
            return commonReturn;
        }

        MemberManager memberManager = new MemberManager();
        CustomerInfoValue[] custInfos = memberManager.queryCusotmerInfo(custId, custName);
        if (CommonConst.ResultCode.SUCCESS == custInfos[0].getCommonReturn().getResultCode()) {

            int realCustId = custInfos[0].getCustomerId();
            CardInfoValue[] cardInfos = queryCardInfo(realCustId);
            //当客户名下无卡的时候才能进行购卡
            if (CommonConst.ResultCode.NO_DATA_EXIST == cardInfos[0].getCommonReturn().getResultCode()) {

                CardInfoValue cardInfoValue = createNewCardByCardType(cardType);
                cardInfoValue.setCustomerId(realCustId);

                if (cardInfoDAO.insert(cardInfoValue) > 0) {
                    commonReturn.setIsSuccess(true);
                    commonReturn.setResultCode(CommonConst.ResultCode.SUCCESS);
                    commonReturn.setResultMessage("You buy card success");
                    return commonReturn;

                } else {

                    commonReturn.setIsSuccess(false);
                    commonReturn.setResultCode(CommonConst.ResultCode.FAILED);
                    commonReturn.setResultMessage("Insert cardInfo failed !");
                    return commonReturn;

                }

            } else if (CommonConst.ResultCode.SUCCESS == cardInfos[0].getCommonReturn().getResultCode()) {
                //查询到卡数据代表已经拥有一张卡
                commonReturn.setIsSuccess(true);
                commonReturn.setResultCode(CommonConst.ResultCode.DATA_NOT_EXPECT);
                commonReturn.setResultMessage("You already have a card !");
                return commonReturn;
            } else if (CommonConst.ResultCode.DATA_NOT_EXPECT == cardInfos[0].getCommonReturn().getResultCode()) {
                //卡片过期
                commonReturn.setIsSuccess(true);
                commonReturn.setResultCode(CommonConst.ResultCode.DATA_NOT_EXPECT);
                commonReturn.setResultMessage(cardInfos[0].getCommonReturn().getResultMessage());
                return commonReturn;
            }
        }
        //库里有重名需要再输入客户编号进行查询
        if (CommonConst.ResultCode.DATA_NOT_EXPECT == custInfos[0].getCommonReturn().getResultCode()) {
            commonReturn.setIsSuccess(false);
            commonReturn.setResultCode(CommonConst.ResultCode.DATA_NOT_EXPECT);
            commonReturn.setResultMessage("There are the same names need customerId");
            return commonReturn;
        }
        commonReturn.setIsSuccess(true);
        commonReturn.setResultCode(CommonConst.ResultCode.NO_DATA_EXIST);
        commonReturn.setResultMessage("Cannot find customer info !");
        return commonReturn;
    }
    //构造新卡
    public static CardInfoValue createNewCardByCardType(String cardType) {
        logger.info("CardManager.createNewCardByCardType Start ");
        CardInfoValue cardInfo = new CardInfoValue();
        cardInfo.setCardType(cardType);
        cardInfo.setState(CommonConst.State.VALIDATE);

        if (CommonConst.YEAR_CARD.equals(cardType)) {

            cardInfo.setUseTimes(CommonConst.YEAR_CARD_TIMES);
            Date dateNow = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateNow);
            calendar.add(Calendar.YEAR, CommonConst.YEAR_CARD_EXPIRE_DATE);
            cardInfo.setExpireDate(sdf.format(calendar.getTime()));
        }
        if (CommonConst.TIMES_CARD.equals(cardType)) {
            cardInfo.setUseTimes(CommonConst.TIMES_CARD_TIMES);
            Date dateNow = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateNow);
            calendar.add(Calendar.YEAR, CommonConst.TIMES_CARD_EXPIRE_DATE);
            cardInfo.setExpireDate(sdf.format(calendar.getTime()));

        }
        return cardInfo;
    }
    //检验卡片是否过期
    public static boolean checkExpireDate(CardInfoValue cardInfoValue) {
        logger.info("CardManager.checkExpireDate Start ");
        boolean isExpired = true;
        String expireDate = cardInfoValue.getExpireDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date expDate = sdf.parse(expireDate);
            Date dateNow = new Date(System.currentTimeMillis());
            if (dateNow.before(expDate)) {
                isExpired = false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return isExpired;
    }
}
